using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode()]
[AddComponentMenu("GK/Sequencer/EditorView/gkSequencerEditorView")]
/// \class  gkSequencerEditorView
/// \brief 	The action sequencer view
public class gkSequencerEditorView : MonoBehaviour
{
#if UNITY_EDITOR
	/// The scroll position
	Vector2 m_f2ScrollPositionVertical;
	
	/// The scroll position
	Vector2 m_f2ScrollPositionHorizontal;
	
	/// The time slider value
	float m_fTimeSliderValue;
	
	/// The sequencer
	gkSequencer m_rSequencer;
	
	// The add remove buttons
	private Rect m_oAddButtonRectangle = new Rect(5, 5, 20, 20);
	private Rect m_oDeleteButtonRectangle = new Rect(24, 5, 20, 20);
	
	// The time length
	private float m_fTimeLength = 2000.0f;
	
	/// \brief  Called after script loading
	void Start()
	{
		// Grab the sequencer
		m_rSequencer = GetComponent<gkSequencer>();
	}
	
	/// \brief  Called on sequencer GUI update
	public void DisplaySequencerGUI()
	{	
		// Display the add track GUI
		DisplayAddRemoveTrackGUI();
		
		// Display the sequence
		m_f2ScrollPositionHorizontal = GUILayout.BeginScrollView(m_f2ScrollPositionHorizontal);
		{
			// Display the time bar
			DisplayTimeBar();
			
			m_f2ScrollPositionVertical = GUILayout.BeginScrollView(m_f2ScrollPositionVertical);
			{
				// Display the tracks
				DisplayTracks();
			}
			GUILayout.EndScrollView();
		}
		GUILayout.EndScrollView();
	}
	
	/// \brief  Display the GUI used to add new track
	void DisplayAddRemoveTrackGUI()
	{
		GUILayout.BeginHorizontal();
		{
			// Button to add a new track
			if (GUILayout.Button(new GUIContent("+", null, "Add a new track"), GUILayout.Width(m_oAddButtonRectangle.width), GUILayout.Height(m_oAddButtonRectangle.height)))
			{
				AddTrack();
			}
	
			// Button to delete the current track
			if (GUILayout.Button(new GUIContent("-", null, "Delete current track"), GUILayout.Width(m_oDeleteButtonRectangle.width), GUILayout.Height(m_oDeleteButtonRectangle.height)))
			{
				DeleteLastTrack();
			}
		}
		GUILayout.EndHorizontal();
	}
	
	/// \brief  Display the tracks GUI
	void DisplayTracks()
	{
		gkSequencerTrack[] rTracks;
		
		// Get the tracks
		rTracks = GetComponentsInChildren<gkSequencerTrack>();
		
		// Display all the sequence tracks
		foreach(gkSequencerTrack rTrack in rTracks)
		{
			rTrack.SendMessage("DisplaySequencerGUI", SendMessageOptions.DontRequireReceiver);
		}
	}
	
	/// \brief  Display the GUI used to add new track
	void DisplayTimeBar()
	{
		// The time slider
		m_fTimeSliderValue = GUILayout.HorizontalSlider(m_fTimeSliderValue, 0.0f, 1.0f, GUILayout.Width(m_fTimeLength));
	}
	
	/// \brief  Add a track
	void AddTrack()
	{
		m_rSequencer.AddTrack();
	}
	
	/// \brief  Delete last track
	void DeleteLastTrack()
	{
		m_rSequencer.DeleteLastTrack();
	}
#endif
}